package com.cdream.basic.algorithm.leetcode.map;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cdream
 * @date 11/8/21
 */
public class Question299 {
    public String getHint(String secret, String guess) {
        // 第一遍循环可以确定A的个数
        int x = 0;
        Map<Character,Integer> map1 = new HashMap<>();
        Map<Character,Integer> map2 = new HashMap<>();

        for(int i = 0; i<secret.length(); i++){
            if(secret.charAt(i)==guess.charAt(i)){
                x++;
            }
            map1.merge(secret.charAt(i),1,(a,b)-> a +b);
            map2.merge(guess.charAt(i),1,(a,b)-> a +b);

        }
        // 第二遍循环可以求出所有相同的个数
        int y = 0;
        for(Character key: map1.keySet()){
            Integer v2 = map2.getOrDefault(key,0);
            Integer v1 = map1.getOrDefault(key,0);
            y += Math.min(v2,v1);
        }
        return x + "A" + (y-x) + "B";
    }

    public static void main(String[] args) {
        new Question299().getHint("1807","7810");
    }
}
